package com.visikard.ui.fragment;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import android.widget.Toast;

import com.visikard.application.UIMessage;
import com.visikard.application.VKRegApplication;
import com.visikard.asynctask.BaseTask;
import com.visikard.asynctask.GetGiftKardTask;
import com.visikard.item.ItemGiftKardRedeem;
import com.visikard.ui.R;
import com.visikard.ui.activity.PunchRedeemActitivy;
import com.visikard.ui.activity.RingUpRedeemActivity;
import com.visikard.utility.Utils;

public class InputCodeFragment extends BaseFragment {

	private EditText edtRedemptionCode;

	/*
	 * stask loader
	 */
	private String mRedemptionCode;
	private Context mContext;

	private ArrayList<ItemGiftKardRedeem> mListgiftKards;

	private BaseTask mAsyncTask;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View parent = inflater.inflate(R.layout.input_code_main_fragment, container, false);

		mContext = getActivity();
		RedeemptionFragment.isScanCode = false;
		innitControl(parent);

		return parent;
	}

	private void innitControl(View parent) {
		edtRedemptionCode = (EditText) parent.findViewById(R.id.edt_redemption_code);
		edtRedemptionCode.setOnEditorActionListener(new OnEditorActionListener() {

			@Override
			public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {

				if (actionId == EditorInfo.IME_ACTION_SEND) {
					showKeyboard(false);
					mRedemptionCode = edtRedemptionCode.getText().toString().trim();
					if (mRedemptionCode != "" && mRedemptionCode.length() > 0) {
						if (Utils.isValidEmail(mRedemptionCode) || mRedemptionCode.contains("USERID")) {
							gotoRedeemGiftKard(true);
						}
						else {
							if (VKRegApplication.uid == -1 || VKRegApplication.loginSession == null) {
								Toast.makeText(getActivity(), R.string.login_lost, Toast.LENGTH_LONG).show();
							}
							else {
								mAsyncTask = new GetGiftKardTask(mHandler);
								mAsyncTask.execute(VKRegApplication.uid, mRedemptionCode);
							}
						}

					}
					else {
						mHandler.sendEmptyMessage(UIMessage.MSG_NOT_INPUT_CODE);
					}
				}
				return false;
			}
		});
	}

	private void showKeyboard(boolean show) {
		if (show)
			getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
		else
			getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
	}

	protected void gotoPunchGiftKard() {
		Intent intent = new Intent(getActivity(), PunchRedeemActitivy.class);
		intent.putExtra(PunchRedeemActitivy.EXT_GIFT_KARD_ITEM, mListgiftKards.get(0));
		startActivity(intent);
		getActivity().overridePendingTransition(R.anim.anim_slide_in_from_right, R.anim.anim_slide_out_from_right);
	}

	private void gotoRedeemGiftKard(boolean isPutEmail) {
		edtRedemptionCode.setText("");
		Intent intent = new Intent(getActivity(), RingUpRedeemActivity.class);

		intent.putExtra(RingUpRedeemActivity.EXT_GIFT_KARD_ITEM, mListgiftKards.get(0));

		startActivity(intent);
		getActivity().overridePendingTransition(R.anim.anim_slide_in_from_right, R.anim.anim_slide_out_from_right);
	}

	@SuppressWarnings("unchecked")
	@Override
	public boolean handleMessage(Message msg) {
		AlertDialog mDialog;
		switch (msg.what) {
			case UIMessage.MSG_GET_DATA_SUCCESS:
				mListgiftKards = (ArrayList<ItemGiftKardRedeem>) msg.obj;
				if (mListgiftKards != null && mListgiftKards.size() > 0) {
					// branch here
					ItemGiftKardRedeem item = mListgiftKards.get(0);
					if (item.getCampaignType() == 8 && item.getActiveFunkKard() == 0) {
						// go to punch detail
						gotoPunchGiftKard();
					}
					else {
						gotoRedeemGiftKard(false);
					}
					// clear text
					edtRedemptionCode.setText("");
				}
				return true;
			case UIMessage.MSG_NOT_INPUT_CODE:
				mDialog = new AlertDialog.Builder(mContext).setIcon(android.R.drawable.ic_dialog_info)
						.setMessage(R.string.ringup_not_input_code)
						.setPositiveButton(R.string.literal_ok, new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog, int which) {
								dialog.cancel();
							}
						}).create();

				mDialog.show();
				return true;
			case UIMessage.MSG_INPUT_WRONG_CODE:
				mDialog = new AlertDialog.Builder(mContext).setIcon(android.R.drawable.ic_dialog_info)
						.setMessage(R.string.ringup_input_wrong_code)
						.setPositiveButton(R.string.literal_ok, new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog, int which) {
								dialog.cancel();
							}
						}).create();

				mDialog.show();
				return true;
		}
		return super.handleMessage(msg);
	}

}
